package org.aiforum.backend.controller;

import java.util.List;

import org.aiforum.backend.common.QueryPreperty;
import org.aiforum.backend.common.Response;
import org.aiforum.backend.service.ReplyService;
import org.aiforum.domain.Reply;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


@Controller
@RequestMapping("/reply")
public class ReplyController {
    @Autowired
	private ReplyService replyService;
    
    @RequestMapping(value={"/query"})
    public ModelAndView query(QueryPreperty queryPreperty,PageInfo page){
    	
    	PageHelper.startPage(page.getPageNum(), page.getPageSize(), true);
    	
    	List<Reply> replys=replyService.queryByPreperty(queryPreperty);
    	page=new PageInfo<Reply>(replys);
    	
    	ModelAndView modelAndView=new ModelAndView();
    	modelAndView.setViewName("reply/list");
    	modelAndView.addObject("replys", replys);
    	modelAndView.addObject("queryPreperty", queryPreperty);
    	modelAndView.addObject("page", page);
    	return modelAndView;
    }
    
    @RequestMapping(value={"/find/{replyId}"},method={RequestMethod.GET})
    public ModelAndView find(@PathVariable Integer replyId){
    	Reply reply=replyService.find(replyId);
    	
    	ModelAndView modelAndView=new ModelAndView();
    	modelAndView.setViewName("reply/details");
    	modelAndView.addObject("reply", reply);
    	return modelAndView;
    }
    
    @RequestMapping(value={"/delete/{replyId}"},method={RequestMethod.GET})
    public ModelAndView delete(@PathVariable Integer replyId){
        Response response=replyService.delete(replyId);
    	ModelAndView modelAndView=new ModelAndView();
    	modelAndView.setViewName("redirect:../../message");
    	modelAndView.addObject("message", response.getMessage());
    	return modelAndView;
    }
}
